/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package q2;

import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author c0909h1200
 */
public class ThreadTwo extends Thread{
     Thread t;

      Data d;
    public ThreadTwo(Data d){
         //use data with thread 1
        this.d = d;

        //init thread 2
        t = new Thread(this);
        t.start();

    }


    @Override
    public void run(){
         //run forever
       while (true) {
            //synchronise
           synchronized(d) {

               if (d.flag) {
                   //display name
                   d.displayName();

                   //change flag
                   d.flag = false;

                   //notify
                   d.notify();
               }

               //wait if busy
               try {
                    d.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadTwo.class.getName()).log(Level.SEVERE, null, ex);
                }

           }

       }
    }

}
